<template>

    <el-button type="success" plain @click="OpenAddUser">
        新增
    </el-button>

    <div style="border-style:groove;border-width: 2px;margin-bottom: 20px;height: 550px;">
        <el-table :data="tableData" border style="width: 100%;height: 500px;">
            <el-table-column type="index" label="编号" width="55px" />
            <el-table-column prop="menuName" label="菜单名称" />
            <el-table-column prop="url" label="菜单Url" />
            <el-table-column prop="parentName" label="父级菜单名称" />
            <el-table-column prop="createUserName" label="创建人" />
            <el-table-column prop="createTime" label="创建日期" width="100px">
                <template v-slot="scoped">
                    {{ moment(scoped.row.createTime).format("YYYY-MM-DD HH:mm:ss") }}
                </template>
            </el-table-column>
            <el-table-column prop="upUserName" label="修改人">
                <template v-slot="scoped">
                    {{ scoped.row.upUserName == null ? "" : scoped.row.upUserName }}
                </template>
            </el-table-column>
            <el-table-column prop="upDataTime" label="修改日期" width="100px">
                <template v-slot="scoped">
                    {{ scoped.row.upDataTime == null ? "" :
                        moment(scoped.row.upDataTime).format("YYYY-MM-DD HH:mm:ss ") }}
                </template>
            </el-table-column>
            <el-table-column prop="address" label="操作" width="200px">
                <template v-slot="scoped">
                    <el-button type="danger" plain @click="Del(scoped.row.id)">删除</el-button>
                    <el-button type="primary" plain @click="openUpdata(scoped.row)">修改</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <div class="demo-pagination-block">


            <el-pagination v-model:current-page="PageData.PageIndex" v-model:page-size="PageData.PageSize"
                :page-sizes="[15, 20, 25, 30]" :background="true
                    " layout="total, sizes, prev, pager, next, jumper" :total="PageData.count" prev-text="上一页"
                next-text="下一页" />
        </div>
    </div>




    <!-- 新增 -->
    <el-dialog v-model="dialogFormVisible" title="新增" width="500">
        <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" :rules="rules" label-width="auto">
            <el-form-item label="父级菜单" prop="pid">
                <el-select v-model="ruleForm.pid" placeholder="please select your zone">
                    <el-option label="顶级语句" :value="0" />
                    <el-option v-for="item in finddata" :label="item.menuName" :value="item.id" />
                </el-select>
            </el-form-item>


            <el-form-item label="菜单名称" prop="menuName">
                <el-input v-model="ruleForm.menuName" />
            </el-form-item>
            <el-form-item label="跳转的Url地址" prop="url">
                <el-input v-model="ruleForm.url" />
            </el-form-item>




            <el-form-item>
                <el-button type="primary" @click="submitForm(ruleFormRef)">
                    保存
                </el-button>
                <el-button @click="resetForm(ruleFormRef)">重置</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>

    <!-- 修改 -->
    <el-dialog v-model="Update" title="修改" width="500">
        <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" :rules="rules" label-width="auto">
            <el-form-item label="父级菜单" prop="pid">
                <el-select v-model="ruleForm.pid" placeholder="please select your zone">
                    <el-option label="顶级语句" :value="0" />
                    <el-option v-for="item in finddata" :label="item.menuName" :value="item.id" />
                </el-select>
            </el-form-item>


            <el-form-item label="菜单名称" prop="menuName">
                <el-input v-model="ruleForm.menuName" />
            </el-form-item>
            <el-form-item label="跳转的Url地址" prop="url">
                <el-input v-model="ruleForm.url" />
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="UpdatesubmitForm(ruleFormRef)">
                    保存
                </el-button>
                <el-button @click="resetForm(ruleFormRef)">重置</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>


</template>

<script lang="ts" setup>
import axios from 'axios';
import { readB } from '@/http/axiosHelper'
import { onMounted, ref, watch, reactive } from 'vue';
import moment from 'moment';

const dialogFormVisible = ref(false)
const Update = ref(false)
const formLabelWidth = '140px'

const tableData = ref([]);
const PageData = ref({
    PageIndex: 1,
    PageSize: 15,
    count: 0,
})




const ShowMenus = () => {
    readB.get("/api/Menus/QueryMenus", {
        params: {
            PageIndex: PageData.value.PageIndex,
            PageSize: PageData.value.PageSize
        }
    })
        .then(res => {
            if (res.data.code == 200) {
                tableData.value = res.data.pageData;
                PageData.value.count = res.data.count;

            }
        })
}


const openUpdata = async (row: any) => {
    await Find()
    Object.assign(ruleForm, row);
    console.log(ruleForm);
    Update.value = true;
}


//-----添加

import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import { dataType } from 'element-plus/es/components/table-v2/src/common.mjs';






const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive({
    "menuName": "",
    "url": "",
    "pid": 0,
    "userId": localStorage.getItem("id"),
    "id": 0,
})



const locationOptions = ['Home', 'Company', 'School']

const rules = reactive<FormRules>({
    url: [
        { required: true, message: '路由不能为空', trigger: 'blur' },

    ],

    menuName: [
        { required: true, message: '菜单名称不能为空', trigger: 'blur' },
    ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            console.log(ruleForm);

            readB.post("/api/Menus/AddMenus", ruleForm)
                .then(res => {
                    if (res.data.code == 200) {
                        ElMessage.success(res.data.msg);
                        dialogFormVisible.value = false
                        ShowMenus();
                    }
                    else {
                        ElMessage.error(res.data.msg);
                    }
                })
        } else {
            console.log('error submit!', fields)
        }
    })
}

const UpdatesubmitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            console.log(ruleForm);

            readB.post("/api/Menus/UpMenus", ruleForm)
                .then(res => {
                    if (res.data.code == 200) {
                        ElMessage.success(res.data.msg);
                        Update.value = false
                        ShowMenus();

                    }
                    else {
                        ElMessage.error(res.data.msg);
                    }
                })
        } else {
            console.log('error submit!', fields)
        }
    })
}

const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
}

const options = Array.from({ length: 10000 }).map((_, idx) => ({
    value: `${idx + 1}`,
    label: `${idx + 1}`,
}))

const OpenAddUser = () => {
    ruleForm.menuName = "";
    ruleForm.pid = 0;
    ruleForm.url = "";
    Find();
    dialogFormVisible.value = true


}





onMounted(() => {
    ShowMenus();
})

watch(() => { PageData.value.PageIndex, PageData.value.PageSize }, () => {
    ShowMenus();
}, {
    deep: true
})



import { ElMessageBox } from 'element-plus'
const Del = (id: any) => {
    ElMessageBox.confirm(
        '确认删除么?',
        '警告',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            readB.get("/api/Menus/QueryIsList", {
                params: {
                    Id: id
                }
            })
                .then(res => {


                    if (res.data.msg == "有子集") {

                        Del01(id);
                    }
                    else {

                        delFirst(id);
                    }
                })
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '已取消',
            })
        })
}

const Del01 = (id: any) => {
    ElMessageBox.confirm(
        '有子集确认删除么?',
        '警告',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            readB.post("/api/Menus/DelMenus", null, {
                params: {
                    Id: id
                }
            })
                .then(res => {
                    if (res.data.code == 200) {
                        ElMessage.success(res.data.msg);
                        ShowMenus();
                    }
                    else {
                        ElMessage.error(res.data.msg);
                    }
                })
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '已取消',
            })
        })
}

const delFirst = (id: any) => {
    readB.post("/api/Menus/DelMenus", null, {
        params: {
            Id: id
        }
    })
        .then(res => {
            if (res.data.code == 200) {
                ElMessage.success(res.data.msg);
                ShowMenus();
            }
            else {
                ElMessage.error(res.data.msg);
            }
        })
}

const finddata = ref([{
    menuName: "",
    id: ""
}])
const Find = () => {
    readB.get("/api/Menus/GetMenus")
        .then(res => {
            finddata.value = res.data.data
        })
}

</script>